package io.xxx.omni.osc.sync

import com.alibaba.fastjson.JSONObject
import io.xxx.omni.osc.common.dateTimeFormatter
import io.xxx.omni.osc.common.toJSONObject
import io.xxx.omni.osc.common.toLocalDateTime
import io.xxx.omni.osc.domain.Document
import io.xxx.omni.osc.domain.DocumentType
import io.xxx.omni.osc.domain.PlatformIdentity
import io.xxx.omni.osc.domain.Store
import io.xxx.omni.osc.sdk.DyClient
import org.springframework.stereotype.Component
import java.time.LocalDateTime
import java.util.*

/**
 * 抖音
 */
// https://op.jinritemai.com/docs/guide-docs/10/23
abstract class DyPorter : GenericPageDocumentPorter(), PlatformIdentity.DouYin {

    protected abstract val method: String

    override val startPage = 0

    override fun getParameters(store: Store): Pair<List<Any>, Boolean> = listOf("update_time") to false

    override fun getResponse(context: JobContext, pageNo: Int, hasTotal: Boolean): JSONObject {
        val (store, startTime, endTime, parameter) = context
        val request = buildRequest(startTime, endTime, parameter, pageNo)
        val client = applicationContext.getBean(DyClient::class.java)
        val response = client.execute(store, method, request)
        if (response.getInteger("err_no") != 0) {
            throwException(context, method, response)
        }
        return response.getJSONObject("data")
    }

    override fun getCount(context: JobContext, response: JSONObject): Int = response.getInteger("total")

    private fun buildRequest(
        startTime: LocalDateTime,
        endTime: LocalDateTime,
        parameter: Any?,
        pageNo: Int,
    ): Map<String, Any> {
        return TreeMap(
            mapOf(
                "is_desc" to "1",
                "order_by" to parameter!!,
                "start_time" to startTime.format(dateTimeFormatter),
                "end_time" to endTime.format(dateTimeFormatter),
                "page" to pageNo,
                "size" to pageSize,
            )
        )
    }
}

// 订单
// https://op.jinritemai.com/docs/api-docs/15/55
@Component
class DyTradePorter : DyPorter() {

    override val documentType = DocumentType.TRADE

    override val method = "order.list"

    override fun buildDocuments(context: JobContext, response: JSONObject): List<Document> {
        return response
            .getJSONArray("list")
            .map {
                val value = it.toJSONObject()
                val sn = value.getString("order_id")
                val status = value.getString("status")
                val modified = value.getString("update_time").toLocalDateTime()
                Document(sn, status, modified, value)
            }
    }
}

// 退单
// https://op.jinritemai.com/docs/api-docs/17/80
@Component
class DyRefundPorter : DyPorter() {

    override val documentType = DocumentType.REFUND

    override val method = "refund.orderList"

    override fun buildDocuments(context: JobContext, response: JSONObject): List<Document> {
        return response
            .getJSONArray("list")
            .map {
                val value = it.toJSONObject()
                val sn = value.getString("aftersale_id")
                val rsn = value.getString("order_id")
                val status = value.getString("status")
                val modified = value.getString("update_time").toLocalDateTime()
                Document(sn, rsn, status, modified, value)
            }
    }
}